package com.raccoonagents.tools.cascet2d.model.stage;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.Display;

import com.raccoonagents.tools.cascet2d.interfaces.IContainer;
import com.raccoonagents.tools.cascet2d.interfaces.IGraphicContainer;
import com.raccoonagents.tools.cascet2d.interfaces.IRenderableStageElement;
import com.raccoonagents.tools.cascet2d.interfaces.IUpdateable;
import com.raccoonagents.tools.cascet2d.model.Vector2f;
import com.raccoonagents.tools.cascet2d.util.FWLogger;

public abstract class Root implements IUpdateable, IGraphicContainer{
	protected final List<IRenderableStageElement> children = new ArrayList<IRenderableStageElement>();

	
	private final void getElements(final IRenderableStageElement elem, final List<IRenderableStageElement> elements){
		elements.add(elem);
		if(IContainer.class.isAssignableFrom(elem.getClass())){
			final IContainer c = (IContainer) elem;
			for(final IRenderableStageElement child : c.getChildren()){
				getElements(child, elements);
			}
		}
	}
	
	final List<IRenderableStageElement> elems = new ArrayList<IRenderableStageElement>();

	protected final List<IRenderableStageElement> getElementsRecursive(){	 		
		elems.clear();
		for(final IRenderableStageElement child : children){
			getElements(child, elems);
		}
		
		return elems;
	}
	
	public Vector2f getSize(){
		return new Vector2f(Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
	}


	@Override
	public final void addChild(final IRenderableStageElement elem){
		if(!children.contains(elem)){
			elem.setParentElement(this);			
		}else{
			FWLogger.warning("attempted to add child " + elem + " to " + this);
		}
		
		children.add(elem);
	}

	@Override
	public final List<IRenderableStageElement> getChildren() {
		return children;
	}
	
	@Override
	public final void removeChild(final IRenderableStageElement elem){
		children.remove(elem);
	}

	@Override
	public Vector2f getLocation() {
		return new Vector2f(0.0f,0.0f);
	}

	@Override
	public final IRenderableStageElement getParentElement() {
		return null;
	}

	@Override
	public Vector2f getWorldCoordinates(final Vector2f localCoordinates) {
		return localCoordinates;
	}

	@Override
	public final void setLocation(final Vector2f location) {
		
	}

	@Override
	public final void setParentElement(final IRenderableStageElement element) {
	}


}
